<?php

/**
 * 微商城分类页
*/
define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');
$c_id = !empty($_GET['c_id']) ? intval($_GET['c_id']) : 0;
$get_sort  = !empty($_REQUEST['sort']) ? $_GET['sort'] : 'goods_id';
$get_order  = !empty($_REQUEST['order']) ? $_GET['order'] : 'DESC';

if ($_SESSION['user_id'] > 0)
{
	$smarty->assign('user_name', $_SESSION['user_name']);

}
$_SESSION['user_id']=1;  //测试用


/* 换一批看看功能 */
if($_REQUEST['act']=='change_vedio'){

   $coursr_id = $_REQUEST['course_id'];
   $vedio_list=get_course_vedio($coursr_id,'sj');
   

$str='';
if(!empty($vedio_list)){   
    foreach($vedio_list as $k=>$v){
    	 $str .='<div class="y1" style="width:50%; float:left;border-top:1px solid #ddd;"><a href="wx_vedio.php?id='.$v['veido_id'].'"><div class="model1" ><div class="imgge1" style="background: url('.$v['thumb_img'].') no-repeat top center;background-size: auto 100%;"></div><div class="text1">'.$v['title'].'</div>
        <div class="text2">'.$v['description'].'</div></div></a>
 </div>';
    }

   echo json_encode($str);exit;

}

}


if ($c_id <= 0)
{
	
}
else
{   
	$sub_cat_array = get_level2_cat($c_id);  //取出当前分类的子分类
    //print_r($cat_array);exit;
	$smarty->assign('c_id', $c_id);
	$cat_name = $db->getOne('SELECT title FROM ' . $ecs->table('course') . ' WHERE course_id=' . $c_id);
	$smarty->assign('cat_name', encode_output($cat_name));
    
	$order_rule='';
   
	$course_vedios = get_course_vedio($c_id,$order_rule); //课程下的视频 
	//print_r($course_vedios);exit;
  //$smarty->assign('vedio_list',$course_vedios);

	$num = count($course_vedios);

	if ($num > 0)
	{
		$page_num = '6';
		$page = !empty($_GET['page']) ? intval($_GET['page']) : 1;
		$pages = ceil($num / $page_num);
		if ($page <= 0)
		{
			$page = 1;
		}
		if ($pages == 0)
		{
			$pages = 1;
		}
		if ($page > $pages)
		{
			$page = $pages;
		}

		$i = 1;
    foreach ($course_vedios as $vedio_data)
    {
      if (($i > ($page_num * ($page - 1 ))) && ($i <= ($page_num * $page)))
      {
        

       $data[]= $vedio_data;

      }
      $i++;
    }
		$smarty->assign('vedio_list', $data);	 //分完页的数据
		$pagebar = get_wap_pager($num, $page_num, $page, 'wx_category.php?c_id='.$c_id.'&sort='.(empty($get_sort)?0:$get_sort).'&order='.(empty($get_order)?0:$get_order), 'page');
		
		$smarty->assign('pagebar', $pagebar);
	}


	

	$smarty->assign('sub_cat_array', $sub_cat_array); //二级分类

}
$smarty->assign('sort', $get_sort);
$smarty->assign('order', $get_order);
$smarty->assign('footer', get_footer());
$smarty->display('wx_category.dwt');



/* 获取二级分类 */
function get_level2_cat($cat_id){
  $sql="select * from {$GLOBALS['ecs']->table('course')} where parent_id='$cat_id' order by sort_order asc";
  $cats_list=$GLOBALS['db']->getAll($sql);
  return $cats_list;
}

/* 获取分类中的视频 */
function get_course_vedio($course_id,$order_rule=''){

  $sql="select GROUP_CONCAT(course_id) as cids from {$GLOBALS['ecs']->table('course')} where parent_id='$course_id'";
  $cids=$GLOBALS['db']->getOne($sql);
 
  if($cids){
  	  if($order_rule=='sj'){
  	  	 $sql="select * from {$GLOBALS['ecs']->table('course_vedio')} where (course_id='$course_id' or course_id in ($cids)) and is_show=1";

  	  }else{
  	  	  $sql="select * from {$GLOBALS['ecs']->table('course_vedio')} where (course_id='$course_id' or course_id in ($cids)) and is_show=1 limit 6";
  	  }
  	
  	}else{

  		if($order_rule=='sj'){

  			 $sql="select * from {$GLOBALS['ecs']->table('course_vedio')} where course_id='$course_id' and is_show=1";
  			}else{

  			    $sql="select * from {$GLOBALS['ecs']->table('course_vedio')} where course_id='$course_id' and is_show=1 limit 6";	
  			}

  		
  	}
   

 
  $arr=$GLOBALS['db']->getAll($sql);
  foreach($arr as $k=>$v){
  	 $arr[$k]['description']=sub_str($v['description'], 12, true);
  }



  if($order_rule=='sj'){
     shuffle($arr); 
     $arr= array_slice($arr,0,6);
   }
  return $arr;
}